import {
	beforeEach,
	afterNotNext
} from "uni-crazy-router"
import {
	VIP_ROUTES
} from '../rolesRoutes.js'
import store from '@/store/index.js'

let intercept

console.log(VIP_ROUTES)

export function bindInterceptLogin() {
	destroyInterceptLogin()

	intercept = beforeEach(async (to, from, next) => {
		if (VIP_ROUTES && VIP_ROUTES.indexOf(to.url) !== -1) {
			if (!store.state.isLogin) {
				uni.showToast({
					title: '没有登录，不能访问！',
					icon: 'none'
				})

				afterNotNext(() => {
					// 拦截路由，并且跳转去登录页
					uni.navigateTo({
						url: '/pages/login/login',
						animationType: 'slide-in-bottom'
					})
				})
			} else {
				next()
			}
			return
		}
		next()
	})
}

export function destroyInterceptLogin() {
	if (intercept) {
		intercept() // 销毁拦截
		intercept = null
	}
}
